<template #detail>
  <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="TbSignRuleForm">
    <a-row>
      <a-col :span="24">
        <a-form-item label="工作日" v-bind="validateInfos.workingDay" id="TbSignRuleForm-workingDay" name="workingDay">
          <j-checkbox type="checkbox" v-model:value="formData.workingDay" dictCode="workingDayType" placeholder="请选择工作日"  allow-clear />
          <span>定义星期几需要打卡</span>
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="上班时间" v-bind="validateInfos.workingTime" id="TbSignRuleForm-workingTime" name="workingTime">
          <time-picker placeholder="请选择上班时间" value-format="HH:mm" format="HH:mm"  v-model:value="formData.workingTime" style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="上班迟到时间" v-bind="validateInfos.workingLateTime" id="TbSignRuleForm-workingLateTime" name="workingLateTime">
          <time-picker placeholder="请选择上班迟到时间" value-format="HH:mm" format="HH:mm"  v-model:value="formData.workingLateTime" style="width: 100%"  allow-clear />
          <span>上班时间之后到迟到时间之前打卡记为迟到</span>
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="上班旷工时间" v-bind="validateInfos.workingAbsenteeismTime" id="TbSignRuleForm-workingAbsenteeismTime" name="workingAbsenteeismTime">
          <time-picker placeholder="请选择上班旷工时间" value-format="HH:mm" format="HH:mm"  v-model:value="formData.workingAbsenteeismTime" style="width: 100%"  allow-clear />
          <span>该时间后打卡记为旷工</span>
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="下班时间" v-bind="validateInfos.offdutyTime" id="TbSignRuleForm-offdutyTime" name="offdutyTime">
          <time-picker placeholder="请选择下班时间" value-format="HH:mm" format="HH:mm"  v-model:value="formData.offdutyTime" style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="下班早退时间" v-bind="validateInfos.earlyDepartureTime" id="TbSignRuleForm-earlyDepartureTime" name="earlyDepartureTime">
          <time-picker placeholder="请选择下班早退时间" value-format="HH:mm" format="HH:mm"  v-model:value="formData.earlyDepartureTime" style="width: 100%"  allow-clear />
          <span>该时间前打卡记为早退，反之记为正常打卡</span>
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="定位中心经纬度" v-bind="validateInfos.orientation" id="TbSignRuleForm-orientation" name="orientation">
          <a-input v-model:value="formData.orientation" placeholder="请输入定位中心经纬度"  allow-clear style="width: 70%" ></a-input>
          <!-- <a href="https://lbs.amap.com/tools/picker/" target="_blank">定位</a> -->
          <a-button type="primary" @click="orientationBtn"> 定位 </a-button>
          <span>获取打卡坐标经纬度，逗号分割</span>
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="不参与打卡人员" v-bind="validateInfos.dropOutMan" id="TbSignRuleForm-dropOutMan" name="dropOutMan">
      <j-select-user v-model:value="formData.dropOutMan"      allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-form-item label="是否展示假期" v-bind="validateInfos.isShowHolidays" id="TbSignRuleForm-isShowHolidays" name="isShowHolidays">
          <j-dict-select-tag type='radio' v-model:value="formData.isShowHolidays" dictCode="yn" placeholder="请选择是否展示假期"  allow-clear />
        </a-form-item>
      </a-col>
      
      <a-col :span="12" v-if="isShow">
        <a-form-item label="元旦假期开始时间" v-bind="validateInfos.newYearDayStartDate" id="TbSignRuleForm-newYearDayStartDate" name="newYearDayStartDate">
          <a-date-picker placeholder="请选择元旦假期开始时间"  v-model:value="formData.newYearDayStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="元旦假期结束时间" v-bind="validateInfos.newYearDayEndDate" id="TbSignRuleForm-newYearDayEndDate" name="newYearDayEndDate">
          <a-date-picker placeholder="请选择元旦假期结束时间"  v-model:value="formData.newYearDayEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="春节假期开始时间" v-bind="validateInfos.springFestivalStartDate" id="TbSignRuleForm-springFestivalStartDate" name="springFestivalStartDate">
          <a-date-picker placeholder="请选择春节假期开始时间"  v-model:value="formData.springFestivalStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="春节假期结束时间" v-bind="validateInfos.springFestivalEndDate" id="TbSignRuleForm-springFestivalEndDate" name="springFestivalEndDate">
          <a-date-picker placeholder="请选择春节假期结束时间"  v-model:value="formData.springFestivalEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="清明假期开始时间" v-bind="validateInfos.qingmingStartDate" id="TbSignRuleForm-qingmingStartDate" name="qingmingStartDate">
          <a-date-picker placeholder="请选择清明假期开始时间"  v-model:value="formData.qingmingStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="清明假期结束时间" v-bind="validateInfos.qingmingEndDate" id="TbSignRuleForm-qingmingEndDate" name="qingmingEndDate">
          <a-date-picker placeholder="请选择清明假期结束时间"  v-model:value="formData.qingmingEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="五一劳动节假期开始时间" v-bind="validateInfos.mayDayStartDate" id="TbSignRuleForm-mayDayStartDate" name="mayDayStartDate">
          <a-date-picker placeholder="请选择五一劳动节假期开始时间"  v-model:value="formData.mayDayStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="五一劳动节假期结束时间" v-bind="validateInfos.mayDayEndDate" id="TbSignRuleForm-mayDayEndDate" name="mayDayEndDate">
          <a-date-picker placeholder="请选择五一劳动节假期结束时间"  v-model:value="formData.mayDayEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="端午假期开始时间" v-bind="validateInfos.dragonBoatFestivalStartDate" id="TbSignRuleForm-dragonBoatFestivalStartDate" name="dragonBoatFestivalStartDate">
          <a-date-picker placeholder="请选择端午假期开始时间"  v-model:value="formData.dragonBoatFestivalStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="端午假期结束时间" v-bind="validateInfos.dragonBoatFestivalEndDate" id="TbSignRuleForm-dragonBoatFestivalEndDate" name="dragonBoatFestivalEndDate">
          <a-date-picker placeholder="请选择端午假期结束时间"  v-model:value="formData.dragonBoatFestivalEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="中秋假期开始时间" v-bind="validateInfos.midAutumnFestivalStartDate" id="TbSignRuleForm-midAutumnFestivalStartDate" name="midAutumnFestivalStartDate">
          <a-date-picker placeholder="请选择中秋假期开始时间"  v-model:value="formData.midAutumnFestivalStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="中秋假期结束时间" v-bind="validateInfos.midAutumnFestivalEndDate" id="TbSignRuleForm-midAutumnFestivalEndDate" name="midAutumnFestivalEndDate">
          <a-date-picker placeholder="请选择中秋假期结束时间"  v-model:value="formData.midAutumnFestivalEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="国庆假期开始时间" v-bind="validateInfos.nationalDayStartDate" id="TbSignRuleForm-nationalDayStartDate" name="nationalDayStartDate">
          <a-date-picker placeholder="请选择国庆假期开始时间"  v-model:value="formData.nationalDayStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="国庆假期结束时间" v-bind="validateInfos.nationalDayEndDate" id="TbSignRuleForm-nationalDayEndDate" name="nationalDayEndDate">
          <a-date-picker placeholder="请选择国庆假期结束时间"  v-model:value="formData.nationalDayEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="其他假期开始时间" v-bind="validateInfos.otherStartDate" id="TbSignRuleForm-otherStartDate" name="otherStartDate">
          <a-date-picker placeholder="请选择其他假期开始时间"  v-model:value="formData.otherStartDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="12" v-if="isShow">
        <a-form-item label="其他假期结束时间" v-bind="validateInfos.otherEndDate" id="TbSignRuleForm-otherEndDate" name="otherEndDate">
          <a-date-picker placeholder="请选择其他假期结束时间"  v-model:value="formData.otherEndDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
        </a-form-item>
      </a-col>
      
      <a-col :span="24"  class="btnCol">
          <a-button @click="isShowClick">{{isShow ? "收起假期安排":"展开假期安排"}}</a-button>
          <a-button type="dashed" @click="synchronizationHolidayClick">同步假期安排</a-button>
          <a-button type="primary" @click="submitForm">保存</a-button>
      </a-col>
    </a-row>
  </a-form>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import { TimePicker } from 'ant-design-vue';
  import JCheckbox from "/@/components/Form/src/jeecg/components/JCheckbox.vue";
  import { getValueType } from '/@/utils';
  import { saveOrUpdate,queryOne,synchronizationHoliday } from '../TbSignRule.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  import dayjs from 'dayjs';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    workingDay: '',   
    workingTime: '',   
    workingLateTime: '',   
    workingAbsenteeismTime: '',   
    offdutyTime: '',   
    earlyDepartureTime: '',   
    orientation: '',   
    dropOutMan: '',   
    isShowHolidays: '',   
    newYearDayStartDate: '',   
    newYearDayEndDate: '',   
    springFestivalStartDate: '',   
    springFestivalEndDate: '',   
    qingmingStartDate: '',   
    qingmingEndDate: '',   
    mayDayStartDate: '',   
    mayDayEndDate: '',   
    dragonBoatFestivalStartDate: '',   
    dragonBoatFestivalEndDate: '',   
    midAutumnFestivalStartDate: '',   
    midAutumnFestivalEndDate: '',   
    nationalDayStartDate: '',   
    nationalDayEndDate: '',   
    otherStartDate: '',   
    otherEndDate: '', 
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  const isShow = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    workingDay: [{ required: true, message: '请输入工作日!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  onMounted(() => {
    queryOne().then((responseResult) =>{
      //console.log("========responseResult===="+JSON.stringify(responseResult))
      edit(responseResult);
    });
  });
  
  function isShowClick(){
    isShow.value = !isShow.value;
  }
  
  function orientationBtn(){
    window.open('https://lbs.amap.com/tools/picker/', '_blank');
  }
  
  function synchronizationHolidayClick(){
    isShow.value = true;
    synchronizationHoliday({year:dayjs(new Date()).format('YYYY-MM-DD')}).then((responseResult) =>{
      console.log("========responseResult===="+JSON.stringify(responseResult));
      if(responseResult){
        responseResult.forEach(item =>{
          /* 除夕春节一切的*/
          if(item.name == "元旦节"){
            formData.newYearDayStartDate = dayjs(item.startday).format('YYYY-MM-DD');
          }else if(item.name == "除夕"){
            formData.springFestivalStartDate = dayjs(item.startday).format('YYYY-MM-DD');
          }else if(item.name == "清明节"){
            formData.qingmingStartDate = dayjs(item.startday).format('YYYY-MM-DD');
          }else if(item.name == "劳动节"){
            formData.mayDayStartDate = dayjs(item.startday).format('YYYY-MM-DD');
          }else if(item.name == "端午节"){
            formData.dragonBoatFestivalStartDate = dayjs(item.startday).format('YYYY-MM-DD');
          }else if(item.name == "国庆节"){
            formData.nationalDayStartDate = dayjs(item.startday).format('YYYY-MM-DD');
          }else if(item.name == "中秋节"){
            formData.midAutumnFestivalStartDate = dayjs(item.startday).format('YYYY-MM-DD');
          }
        })
      }
    });
  }
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding-top: 60px;
    background-color: white;
  }
  .btnCol {
    text-align: center;
  }
</style>
